﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace sdamarketing.Account
{
    public partial class AccountDetails : osnova1
    {
        protected void Page_Load(object sender, EventArgs e)
        {

                if (!IsPostBack)
                {
                    try
                    {
                        if (Session["iduser"] == null || Session["username"] == null)
                        {
                            Response.Redirect("~/Default.aspx");
                        }

                        // AN Gi polnam text boxovite so infrmaciite na userot vo sesijata
                        DataSet user = ReturnDataSet.ReturSet("SELECT firstName, lastName, username, password, eMail, securityQuestion, securityAnswer FROM Users WHERE idUser = '" + Session["idUser"].ToString() + "'");
                        UsernameTextBox.Enabled = false;
                        FirstNameTextBox.Text = user.Tables["DataSet"].Rows[0].ItemArray[0].ToString();
                        LastNameTextBox.Text = user.Tables["DataSet"].Rows[0].ItemArray[1].ToString();
                        UsernameTextBox.Text = user.Tables["DataSet"].Rows[0].ItemArray[2].ToString();
                        //     PasswordTextBox.Text = user.Tables["DataSet"].Rows[0].ItemArray[3].ToString();
                        //     PasswordConfTextBox.Text = user.Tables["DataSet"].Rows[0].ItemArray[3].ToString();
                        EMailTextBox.Text = user.Tables["DataSet"].Rows[0].ItemArray[4].ToString();
                        QuestionTextBox.Text = user.Tables["DataSet"].Rows[0].ItemArray[5].ToString();
                        AnswerTextBox.Text = user.Tables["DataSet"].Rows[0].ItemArray[6].ToString();                    
                    }                    
                    catch (Exception exception)
                    {
                        SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -AccountDetails- ");
                    }
                }
        }
        protected void saveButton_Click(object sender, EventArgs e)
        {
            try
            {
                // AN gi zacuvuvam podatosite so izmenite
                Users.SaveChangesForUser(Int32.Parse(Session["idUser"].ToString()), FirstNameTextBox.Text, LastNameTextBox.Text, UsernameTextBox.Text, PasswordTextBox.Text, EMailTextBox.Text, QuestionTextBox.Text, AnswerTextBox.Text);
                lblMessage.Text = "Податоците се успешно изменети";
            }
            catch (SqlException exception)
            {
                lblMessage.Text = "Податоците не се успешно изменети";
                SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -AccountDetails- saveButton_Click");
            }
            
        }
    }
}
